/*
 * (c)Copyright 北京艾的教育科技股份有限公司
 * http://www.ide365.com/
 */
package com.mall.manager.authority.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mall.untils.MapUtil;

/**
 * 用户角色业务层
 * @ClassName: UsersPartService
 * @Description: 1.1
 * @author:wangwenyue wangwenyue@ide365.com
 * @date 2016年7月11日 上午9:54:34
 */
@Service
public class UsersPartService {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(UsersPartService.class);
	
	
	
	/**
	 * 根据条件（数据库列名）获取详细信息
	 * @param paraMap条件值
	 * @param claim 条件名称
	 * @return
	 */
	public Map<String, Object> getUsersPart(Map<String, Object> paraMap) {
		String claim = MapUtils.getString(paraMap, "claim");
		String res = MapUtils.getString(paraMap, "res");
		String sql = "select * from t_user_part where " + claim + " = ? ";
		try {
			Map<String, Object> map = jdbcTemplate.queryForMap(sql, res);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
			return null;
		}
	}
	
	
	/**
	 * 为用户添加角色   //前台 配合getUsersPart方法使用避免重复插入
	 * @param paraMap
	 * @return
	 */
	public int add_UserByPart(Map<String, Object> paraMap){
		Integer user_id = MapUtil.getInteger(paraMap, "user_id");//获取用户id
		String part_id = MapUtil.getStringValue(paraMap, "part_id");//获取角色id
		try {
			String sql="insert into t_user_part(user_id,part_id,create_time,update_time) values(?,?,now(),now())";
			return jdbcTemplate.update(sql,user_id,part_id);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
		}
		return 0; 
	}
	
	/**
	 * 为用户添加角色   //前台 配合getUsersPart方法使用避免重复插入
	 * @param paraMap
	 * @return
	 */
	public int add_UserByPartList(Map<String, Object> paraMap){
		Integer user_id = MapUtil.getInteger(paraMap, "user_id");//获取用户id
		String part_ids = MapUtil.getStringValue(paraMap, "part_ids");//获取角色ids
		String[] partIdsArr = part_ids.split(","); //获取所有角色id
		String sql = "";
		try {
			
			for (String partid : partIdsArr) {
				sql="insert into t_user_part(user_id,part_id,create_time,update_time) values(?,?,now(),now())";
				int update = jdbcTemplate.update(sql,user_id,partid);
				return update;
			}
		} catch (Exception e) {
			logger.info(e.toString());
			return 0; 
		}
		return 0;
	}
	
	
	
	
	
	
	/**
	 * 删除
	 * @param paraMap
	 * @return
	 */
	public int removeUserPart(Map<String, Object> paraMap) {
		Integer user_id = MapUtil.getInteger(paraMap, "user_id");//获取用户id
		Integer part_id = MapUtil.getInteger(paraMap, "part_id");//获取角色id
		try {
			return jdbcTemplate.update("delete from t_user_part where user_id=? and part_id=?",user_id,part_id);
		} catch (Exception e) {
			logger.info(e.toString());
			return 0; 
		}
	}
	
	/**
	 * 查询用户角色
	 * @param map 封装查询条件
	 * @return
	 */
	public List<Map<String, Object>> findUserPart(Map<String,Object> map){
		try{
			Integer userId = MapUtils.getInteger(map, "id");
			if(userId!=null){
				String sql = "select user_id userId,part_id partId from t_user_part where user_id=?";
				return jdbcTemplate.queryForList(sql,userId);
			}
		}catch(Exception e){
			logger.info("查询用户角色出现异常："+e.toString());
		}
		return null;
	}
	
}
